package com.ouman.luoliluoli;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.Toolbar;
import android.transition.Slide;
import android.transition.TransitionInflater;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.ouman.luoliluoli.utils.ThemeChanger;
import com.ouman.luoliluoli.utils.VendorAppHelper;

public class AboutActivity extends AppCompatActivity {

    private Button buttonSponsor1;
    private Button buttonSponsor2;

    private TextView textViewPart2;
    private TextView textViewPart5;
    private TextView textViewPart3;
    private TextView textViewPart4;

    private CardView cardView1;
    private CardView cardView2;
    private CardView cardView3;
    private CardView cardView4;
    private CardView cardView5;

    private TextView textViewAppName;
    private Button buttonAddQQGroup;

    private int themeColor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ThemeChanger.applyTheme(this, null);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);

        CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsingToolBarLayoutAbout);
        // 把这个设为false才能显示toolbar的title
        collapsingToolbarLayout.setTitleEnabled(false);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_about);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("关于");


        textViewPart2 = (TextView) findViewById(R.id.textViewPart2Title);
        textViewPart3 = (TextView) findViewById(R.id.textViewPart3Title);
        textViewPart4 = (TextView) findViewById(R.id.textViewPart4Title);
        textViewPart5 = (TextView) findViewById(R.id.textViewPart5Title);


        TypedValue typedValue = new TypedValue();
        AboutActivity.this.getTheme().resolveAttribute(R.attr.colorPrimary, typedValue, true);
        themeColor = typedValue.data;
        System.out.println(themeColor);
        System.out.println("[IMPORTANT] got activity primary color: " + themeColor);

        getWindow().setNavigationBarColor(themeColor);
        textViewPart2.setBackgroundColor(themeColor);
        textViewPart3.setBackgroundColor(themeColor);
        textViewPart4.setBackgroundColor(themeColor);
        textViewPart5.setBackgroundColor(themeColor);

        Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_card_show);

        cardView1 = (CardView) findViewById(R.id.cardViewPart1);
        cardView2 = (CardView) findViewById(R.id.cardViewPart2);
        cardView3 = (CardView) findViewById(R.id.cardViewPart3);
        cardView4 = (CardView) findViewById(R.id.cardViewPart4);
        cardView5 = (CardView) findViewById(R.id.cardViewPart5);
        cardView1.startAnimation(animation);
        cardView2.startAnimation(animation);
        cardView3.startAnimation(animation);
        cardView4.startAnimation(animation);
        cardView5.startAnimation(animation);


        final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(AboutActivity.this);
        int colorAccent = preferences.getInt("currentThemeColorAccent", ResourcesCompat.getColor(getResources(), R.color.colorAccent, null));

        AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
        alphaAnimation.setDuration(300);
        alphaAnimation.setStartOffset(600);
        textViewAppName = (TextView) findViewById(R.id.textViewAppName);
        textViewAppName.setText(getVersionName());
        textViewAppName.setTextColor(colorAccent);
        textViewAppName.setAnimation(alphaAnimation);


        buttonSponsor1 = (Button) findViewById(R.id.buttonSponsor1);
        buttonSponsor2 = (Button) findViewById(R.id.buttonSponsor2);
        buttonAddQQGroup = (Button) findViewById(R.id.buttonAddQQGroup);

        buttonSponsor1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ClipboardManager cbm = (ClipboardManager)AboutActivity.this.getSystemService(Context.CLIPBOARD_SERVICE);
                final android.content.ClipData clipData = android.content.ClipData
                        .newPlainText("WeChat", "jintianiloveu");
                cbm.setPrimaryClip(clipData);

                AlertDialog.Builder builder = new AlertDialog.Builder(AboutActivity.this);
                builder.setTitle("感谢");
                builder.setMessage("多谢少侠仗义相助，我们已经把微信号复制到了剪贴板，少侠可以添加我们客服美美的微信通过红包给我们捐助巨款");
                builder.setNegativeButton("点错了", null);
                builder.setPositiveButton("好, 立马去微信", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if(VendorAppHelper.isWeChatAvailible(AboutActivity.this)){
                            Intent intent = getPackageManager().getLaunchIntentForPackage("com.tencent.mm");
                            startActivity(intent);
                        }else {
                            Toast.makeText(AboutActivity.this, "本机貌似没有安装WeChat",Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                builder.show();
            }
        });

        buttonSponsor2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(AboutActivity.this);
                builder.setTitle("感谢");
                builder.setMessage("少侠真的只捐五毛么？现在辣条都要2.5元一包啊, 我们已经把客服美美的微信号复制到了剪贴板, 少侠可以通过微信红包捐助我们");
                builder.setNegativeButton("那捐六毛吧", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        ClipboardManager cbm = (ClipboardManager)AboutActivity.this.getSystemService(Context.CLIPBOARD_SERVICE);
                        final android.content.ClipData clipData = android.content.ClipData
                                .newPlainText("WeChat", "jintianiloveu");
                        cbm.setPrimaryClip(clipData);
                        Toast.makeText(AboutActivity.this, "微信号已复制，给我们赞助后，我们会定期把名单加到致谢里面哦", Toast.LENGTH_SHORT).show();
                        if(VendorAppHelper.isWeChatAvailible(AboutActivity.this)){
                            Intent intent = getPackageManager().getLaunchIntentForPackage("com.tencent.mm");
                            startActivity(intent);
                        }else {
                            Toast.makeText(AboutActivity.this, "本机貌似没有安装WeChat",Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                builder.setPositiveButton("八毛不能再多了", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        ClipboardManager cbm = (ClipboardManager)AboutActivity.this.getSystemService(Context.CLIPBOARD_SERVICE);
                        final android.content.ClipData clipData = android.content.ClipData
                                .newPlainText("WeChat", "jintianiloveu");
                        cbm.setPrimaryClip(clipData);
                        Toast.makeText(AboutActivity.this, "微信号已复制，给我们赞助后，我们会定期把名单加到致谢里面哦", Toast.LENGTH_SHORT).show();
                        if(VendorAppHelper.isWeChatAvailible(AboutActivity.this)){
                            Intent intent = getPackageManager().getLaunchIntentForPackage("com.tencent.mm");
                            startActivity(intent);
                        }else {
                            Toast.makeText(AboutActivity.this, "本机貌似没有安装WeChat",Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                builder.show();
            }
        });
        buttonAddQQGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                System.out.println("点击了加群");
                if (VendorAppHelper.isQQAvailable(AboutActivity.this)){
                    String url11 = "mqqwpa://im/chat?chat_type=group&uin=366590979&version=1";
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url11)));
                } else{
                    Toast.makeText(AboutActivity.this, "本机貌似没有安装QQ",Toast.LENGTH_SHORT).show();
                }
            }
        });


    }

    private void setupWindowAnimations() {
        Slide slideTransition = new Slide();

        slideTransition.setSlideEdge(Gravity.START);

        slideTransition.setDuration(getResources().getInteger(R.integer.anim_duration_long));
        getWindow().setEnterTransition(slideTransition);
        getWindow().setExitTransition(slideTransition);
        //getWindow().setReenterTransition(buildExitTransition());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.about, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_help) {
            return true;
        }else if (id == android.R.id.home){
            AboutActivity.this.finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public String getVersionName() {
        try {
            PackageManager manager = this.getPackageManager();
            PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
            String version = info.versionName;
            return getString(R.string.app_name) + " " + "Version:" + " " + version;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

}
